<template>
  <div class="page-container">
    <SearchForm
        :search-list="searchList"
        :search-params="searchParams"
        @search="onSearch"
        @reset="onReset"
    />
    <div class="table-box">
      <el-row :gutter="12">
      <el-col :span="4" v-for="item in tableData" :key="item.id" style="margin-bottom: 12px">
        <el-card :body-style="{ padding: '0px' }" shadow="none" >
          <img :src="item.image" class="image">
          <div style="padding: 14px;">
            <span>{{ item.title }}</span>
            <div class="bottom clearfix">
              <el-button type="text" class="button" @click="handleItem('like', item)">
                <i class="el-icon-thumb"></i>({{ item.like }})
              </el-button>
              <el-button type="text" class="button" @click="handleItem('collect', item)">
                <i class="el-icon-star-on"></i>{{ item.collect ? '取消收藏' : '收藏' }}
              </el-button>
              <el-button type="text" class="button" @click="handleItem('download', item)">
                <i class="el-icon-download"></i>下载
              </el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
     </div>

    <div class="page-box">
      <el-pagination
          background
          :current-page="pageNo"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalSize"
          @size-change="changePageSize"
          @current-change="changePageNo"
      />
    </div>
  </div>
</template>
<script>

export default {
  name: "index",
  data() {
    return {
      searchList: [
        {
          key: 'nickname',
          name: 'input',
          label: '姓名'
        }
      ],
      searchParams: {
        name: ''
      },
      pageNo: 1,
      pageSize: 12,
      pageSizes: [12, 20, 30, 40, 50],
      totalSize: 0,
      tableData: []
    }
  },
  created() {
    this.onSearch()
  },
  methods: {
    initData() {
      let tableData = [
        {
          id: 1,
          title: '图片A',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 2,
          title: '图片B',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 3,
          title: '图片C',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 4,
          title: '图片D',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 5,
          title: '图片E',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 6,
          title: '图片F',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 7,
          title: '图片G',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 8,
          title: '图片H',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 9,
          title: '图片I',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 10,
          title: '图片J',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 11,
          title: '图片K',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 12,
          title: '图片L',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 13,
          title: '图片M',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 14,
          title: '图片N',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 15,
          title: '图片O',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 16,
          title: '图片P',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 17,
          title: '图片Q',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 18,
          title: '图片R',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 19,
          title: '图片S',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        },
        {
          id: 20,
          title: '图片T',
          image: "https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png",
          like: 0,
          collect: 0
        }
      ]
      // 根据分页，查询条件过滤
      let list = tableData.filter(item => {
        let flag = true
        if (this.searchParams.name) {
          flag = item.title.includes(this.searchParams.name)
        }
        return flag
      })
      // 分页
      let start = (this.pageNo - 1) * this.pageSize
      let end = start + this.pageSize
      this.tableData = list.slice(start, end)
      this.totalSize = tableData.length
      console.log('this.tableData',this.tableData)
    },
    onSearch() {
      console.log('search')
      this.pageNo = 1
      this.initData()
    },
    onReset() {
       this.searchParams = {
         name: ''
       }
    },
    changePageNo(pageNo) {
      this.pageNo = pageNo
      this.initData()
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize
      this.pageNo = 1
      this.initData()
    },
    handleItem(key,item) {
      if (key === 'like') {
        item.like++
        this.$message.success('点赞成功')
      } else if (key === 'collect') {
        if (item.collect) {
          item.collect = 0
          this.$message.success('取消收藏')
        } else {
          item.collect = 1
          this.$message.success('收藏成功')
        }
      } else if (key === 'download') {
        // 处理下载图片
        this.$message.success('下载成功')
        // 前端下载图片
        let a = document.createElement('a')
        a.target = '_blank'
        a.href = item.image
        a.download = item.title
        a.click()
      }
    }
  }
}
</script>
<style scoped lang="scss">

</style>
